const app = getApp();
import request from '../../utils/request'
Page({
  data: {
    userInfo: {},
    sqok: true,
    checked: true,
    showval: false,
    VerificationCode: "获取验证码",
    disabled: false,
    ok: false,
    phone: '',
    currentTime: 60, //验证码描述
    password: '',
    choseinp: [{
      name: '记住我',
      value: '1'
    }],
    checked: '',
    ymurl: app.globalData.ymurl,
    openId: "",
    show: true,
    loginStatus: '',
    show: false,
    signCode: "",
    sms: "",
    showVal: false,
    timer: "",
    Num: '60',
    isDisabled: false,
    login_radio: "1",
    isHide: 0,
    openId: '',
    extraData: {},
    loginAuth: false,
    agentAppId: '',
    userInfoAuth: false,
  },

  onLoad: function () {
    console.log(app.globalData.agentAppId);
    if (app.globalData.agentAppId == '' || !wx.getStorageSync('extraData')) {
      this.setData({
        loginAuth: true,
      })
    } else {
      this.setData({
        loginAuth: false,
        agentAppId: app.globalData.agentAppId,
        extraData: wx.getStorageSync('extraData'),
      })
    }
    if (wx.getStorageSync('userInfo')) {
      this.setData({
        userInfoAuth: true
      })
    }
  },
  prompt() {
    wx.showModal({
      title: '提示',
      content: '请从代理端或者商户端跳转过来再点击按钮',
      showCancel: false,
      complete: (res) => {
        if (res.cancel) {

        }

        if (res.confirm) {

        }
      }
    })
  },
  closesmallBox() {
    this.setData({
      showval: false,
      sendCode: ""
    })
  },

  opnensmallBox: (function (msg) {
    const that = this
    if (that.data.sms == '') {
      wx.showToast({
        title: '请先输入验证码',
        icon: 'none',
        duration: 2500
      })
    } else {
      that.loginRequest()
      that.setData({
        showval: false,
        sms: '',
      })
      wx.showLoading({
        title: '请等待~',
        mask: true,
      })

    }
  }),
  changePwd() {
    wx.navigateTo({
      url: '../forgetPassword/forgetPassword',
    })
  },
  getVerificationCode() {
    var that = this
    if (that.data.phone) {
      that.getCode();
      request({
        url: '/SMS/send',
        data: {
          phone: that.data.phone,
          type: 6,
        },
      }).then(res => {

      })
      that.setData({
        disabled: true
      })
    } else {
      wx.showToast({
        title: '请输入手机号',
        icon: 'none'
      })
    }
  },
  onShow: function () {
    wx.hideHomeButton();
  },
  login_radio(e) {
    this.setData({
      login_radio: e.detail,
    });
  },

  countDown: function () {
    var that = this
    var Num = that.data.Num
    var isDisabled = this.data.isDisabled
    request({
      url: '/SMS/send',
      data: {
        phone: this.data.phone,
        type: 0,
      }
    }).then((res) => {
      if (res.data.code == 200) {
        var timer = setInterval(function () {
          Num -= 1;
          that.setData({
            Num: Num,
            isDisabled: true
          })
          if (Num <= -1) {
            clearInterval(timer)
            that.setData({
              Num: 60,
              isDisabled: false
            })
          }
        }, 1000)
      }
    })

  },
  loginText() {
    this.setData({
      show: !this.data.show
    })
  },
  sendTitle() {

    //    ;
    // })
    setInterval(function () {
      that.setData({
        time: that.data.time--
      })
    }, 1000)
  },
  // 授权登录
  bindGetUserInfo() {
    const that = this;
    console.log('授权');
    wx.login({
      success: (res) => {
        const loginCode = res.code;
        wx.getUserInfo({
          desc: '获取授权',
          success: function (res) {
            request({
              url: '/wechat/login',
              data: {
                code: loginCode,
                encryptedData: res.encryptedData,
                iv: res.iv,
              }
            }).then((res) => {
              wx.hideToast();
              // wx.hideLoading();
              if (res.data.code == 200) {
                wx.setStorageSync('userInfo', res.data.data);
                wx.reLaunch({
                  url: '../withdrawal_agent/withdrawal_agent',
                })
              } else {
                wx.showToast({
                  icon: "none",
                  title: res.data.message,
                  duration: 3500
                })
              }
            })
          },
          fail: function (err) {
            console.log(err);
          }
        })
      },
    })
  },
  jumpWithdrawal_agent() {
    console.log('不授权');
    wx.reLaunch({
      url: '../withdrawal_agent/withdrawal_agent',
    })
  },

  //获取手机号
  getPhoneNumber(e) {
    var that = this
    let encryptedData = e.detail.encryptedData
    let iv = e.detail.iv
    if (!wx.getStorageSync('userInfo') || !that.data.openId) {
      that.setData({
        isHide: 0,
      })
      wx.showModal({
        title: '提示',
        content: '您还没有授权登录，请授权',
        showCancel: false,
        confirmText: '授权',
        complete: (res) => {
          if (res.confirm) {
            that.setData({
              isHide: 1,
            })
          }
        }
      })

    } else {
      wx.login({
        success: res => {
          request({
            url: '/wechat/get/user-phone',
            data: {
              code: res.code,
              encryptedData: encryptedData,
              iv: iv,
              openid: that.data.openId,
            }
          }).then((res) => {
            console.log(res);
            if (res.data.code == 200) {
              wx.hideToast();
              that.setData({
                isHide: 0
              })
              wx.setStorageSync('openIdPhone', res.data.data);
              let userInfo = [];
              userInfo = wx.getStorageSync('userInfo')
              let phone = res.data.data;
              userInfo.phone = phone;
              wx.setStorageSync('userInfo', userInfo);
              that.userlogin();
            } else if (res.data.code == 3003) {
              that.setData({
                isHide: 0
              })
              wx.showToast({
                icon: "none",
                title: res.data.message,
                duration: 3000
              })
            } else {
              that.setData({
                isHide: 0
              })
              wx.showToast({
                icon: "none",
                title: res.data.message,
                duration: 3000
              })
            }
          })
        }
      })
    }
  },

  onClose() {
    this.setData({
      show: false
    });
  },
  getuser: function (e) {
    // console.log(e);
    this.setData({
      phone: e.detail.value
    })
  },
  clickOk() {
    Dialog.confirm({
      title: '标题',
      message: '弹窗内容',
    })
      .then(() => {
        // on confirm
      })
      .catch(() => {
        // on cancel
      });
  },
  getpass: function (e) {
    // console.log(e);
    this.setData({
      password: e.detail.value
    })
  },

  chosepass: function (e) {
    // console.log(e);
    this.setData({
      checked: e.detail.value[0]
    })

    // console.log(e.detail.value[0]);
  },
  onChange(event) {
    this.setData({
      checked: event.detail,
    });
  },
  sms(e) {
    this.setData({
      sms: e.detail.value
    })
  },
  loginRequest() {
    request({
      url: '/account/login',
      data: {
        phone: this.data.phone,
        password: this.data.password,
        code: this.data.sms,
        openid: wx.getStorageSync('userInfo').openid
      },

    }).then((res) => {
      if (res.data.code == 200) {
        wx.showToast({
          icon: "none",
          title: res.data.message,
          duration: 3000,
        })
        wx.setStorageSync('useName', res.data.data.agentName || res.data.data.name)
        wx.setStorageSync('login_radio', "1");
        wx.setStorageSync('roleId', res.data.data.roleId)
        wx.setStorageSync('level', res.data.data.level)
        wx.setStorageSync('login_id', res.data.data.id)
        wx.setStorageSync('withdrawalRateCheck', res.data.data.withdrawalRateCheck)
        wx.setStorageSync('indexList', res.data.data.indexRentBoxVo)
        wx.setStorageSync('authorization', res.data.data.token)
        wx.setStorageSync('depositShare', res.data.data.depositShare == null ? 0 : res.data.data.depositShare)
        wx.setStorageSync('setDepositRateAuth', res.data.data.setDepositRateAuth == null ? 0 : res.data.data.setDepositRateAuth)
        wx.setStorageSync('productAmortization', res.data.data.productAmortization == null ? 0 : res.data.data.productAmortization) //摊销权限 1开 0关
        wx.setStorageSync('timeoutAuth', res.data.data.timeoutAuth)
        wx.setStorageSync('prePayRefundAuth', res.data.data.prePayRefundAuth)

        wx.reLaunch({
          url: '../index/index',
        })
      } else if (res.data.code == 1056) {
        this.setData({
          showval: true
        })
      } else {
        wx.showToast({
          icon: "none",
          title: res.data.message,
          duration: 3000
        })
      }
    })
  },
  // 获取验证码
  getCode() {
    const that = this;
    var currentTime = that.data.currentTime
    this.setData({
      interval: setInterval(function () {
        currentTime--;
        that.setData({
          VerificationCode: currentTime + '秒'
        })
        if (currentTime <= 0) {
          clearInterval(that.data.interval)
          that.setData({
            VerificationCode: '重新发送',
            currentTime: 60,
            disabled: false
          })
        }
      }, 1000)
    })
  },
  userlogin: function () {
    const that = this;
    var phone = this.data.phone;
    wx.setStorageSync('phone', phone)
    var password = this.data.password;
    var checked = this.data.checked;
    if (wx.getStorageSync('userInfo').phone || wx.getStorageSync('openIdPhone')) {
      if (phone == '' || password == '') {
        wx.showToast({
          title: '请输入账号或者密码',
          icon: "none",
          duration: 3000
        })
      } else if (password.length >= 6 && password.length <= 16) {
        if (that.data.login_radio == 1) {
          this.loginRequest()
        } else if (that.data.login_radio == 2) {
          request({
            url: '/customerService/login',
            data: {
              phone: phone,
              password: password,
              openid: wx.getStorageSync('userInfo').openid
            }
          }).then(res => {
            // console.log(res);
            wx.showToast({
              icon: "none",
              title: res.data.message,
              duration: 3000,
            })
            if (res.data.code == 200) {
              wx.setStorageSync('authorization', res.data.data);
              wx.setStorageSync('roleId', "6");
              wx.setStorageSync('login_radio', "2");
              wx.setStorageSync('useName', "客服人员")
              wx.reLaunch({
                url: '../serviceOrders/serviceOrders',
              })
            }
          })
        }

      } else {
        wx.showToast({
          title: '密码长度为6-16个字符',
          icon: "none"
        })
      }
    } else {
      that.setData({
        isHide: 2
      })

    }

    if (checked == 1) {
      // console.log('已勾选')
      wx.setStorageSync("user_nums", phone);
      wx.setStorageSync("password_nums", password);
    } else {
      // console.log('未勾选')
      // wx.setStorageSync("user_nums", '');
      // wx.setStorageSync("password_nums", '');
      wx.removeStorageSync("user_nums");
      wx.removeStorageSync("password_nums");
    }

    wx.setStorageSync("checked", checked);
  },

})